﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class CaiGou_FuKuan_In_Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sql = "select 采购付款登记.ID,采购付款登记.付款单号,应付金额.合同编号,应付金额.名称,应付金额.应付金额,采购付款登记.本次付款,采购付款登记.付款日期 from 应付金额  inner join 采购付款登记 on 应付金额.ID=采购付款登记.合同ID";
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.SqlDataSource3.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource3.SelectCommand = (string)this.ViewState["SelectCommand"];

        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string temp = this.FaPiaoHao.Text.Trim().Replace("'", "");
            string Sql = "";
            Sql = "[应付金额].[合同编号] like '%" + temp + "%'";

            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [采购付款登记].[付款日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [采购付款登记].[付款日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuoDanWei.Text != "0")
            {
                Sql += " and [应付金额].[名称]='" + this.GongHuoDanWei.SelectedItem.Text + "'";
            }
            this.SqlDataSource3.SelectCommand = @"select 采购付款登记.ID,采购付款登记.付款单号,应付金额.合同编号,应付金额.名称,应付金额.应付金额,采购付款登记.本次付款,采购付款登记.付款日期 from 应付金额  inner join 采购付款登记 on 应付金额.ID=采购付款登记.合同ID where " + Sql;

            this.ViewState["SelectCommand"] = this.SqlDataSource3.SelectCommand;


        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView2.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView2.PageIndex > 0) this.GridView2.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView2.PageIndex < this.GridView2.PageCount - 1) this.GridView2.PageIndex++;
                        break;
                    case "++":
                        this.GridView2.PageIndex = this.GridView2.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView2.PageCount)
                    {
                        this.GridView2.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView2.PageIndex + 1).ToString();
            }
        }



        protected void GridView2_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView2.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView2.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView2.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView2.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView2.PageIndex == this.GridView2.PageCount - 1 || this.GridView2.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView2.PageIndex > this.GridView2.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }

        protected void GridView2_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView2_DataBound(null, null);
        }
        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("CaiGou_FuKuan.aspx?ID=" + ((Button)sender).CommandName);
        //}



        //protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    this.GridView2_DataBound(null, null);

        //    GridViewRow row = this.GridView2.SelectedRow;
        //    string i = row.Cells[1].Text;

        //    SqlConnection Conn = new SqlConnection();
        //    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
        //    Conn.Open();

        //    string sql = @"select 付款明细.ID,票据号,付款日期,付款.供货商ID,原料.品名,原料.规格,原料.单位,付款明细.本次付款  from 付款 inner join 付款明细 on 付款明细.付款ID=付款.ID inner join 采购发票明细 on 采购发票明细.ID=付款明细.采购发票明细ID inner join 原料 on 原料.ID=采购发票明细.原料ID where 付款ID=" + i + "and 付款明细.本次付款>0";
        //    SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
        //    DataSet ds = new DataSet();
        //    sda.Fill(ds, "付款明细");
        //    this.GridView3.DataSource = ds;
        //    this.GridView3.DataBind();
        //    Conn.Close();
        //    this.GridView3.Visible = true;
        //}

        

        //protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    int i = e.RowIndex;//行
        //    GridViewRow row = this.GridView2.Rows[i];
        //    string j = row.Cells[1].Text;
        //    SqlConnection Conn = new SqlConnection();
        //    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
        //    Conn.Open();
        //    SqlTransaction Tran = Conn.BeginTransaction();
        //    SqlCommand Cmd = new SqlCommand();
        //    Cmd.Connection = Conn;
        //    Cmd.Transaction = Tran;
        //    try
        //    {
        //        Cmd.CommandText = "YuanLiao_CaiGou_FaPiao_MingXi_Delete";
        //        Cmd.CommandType = CommandType.StoredProcedure;
        //        Cmd.Parameters.Add(new SqlParameter("@采购发票ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
        //        Cmd.Parameters["@采购发票ID"].Value = int.Parse(j);
        //        Cmd.ExecuteNonQuery();
        //        Cmd.Parameters.Clear();
        //        Tran.Commit();
        //    }
        //    catch (Exception err)
        //    {
        //        Tran.Rollback();
        //        this.Message.Text = err.Message;
        //    }
        //    Cmd.Dispose();
        //    Tran.Dispose();
        //    Conn.Close();
        //    Conn.Dispose();
      
        //}

       

        //protected void Edit_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("CaiGou_FuKuan_In.aspx?ID=" + ((Button)sender).CommandName);

        //}

        
        //protected void Button_New_Click(object sender, EventArgs e)
        //{

        //}
        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("CaiGou_FuKuan_In.aspx");

        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView2.SelectedIndex >= 0 && GridView2.Rows.Count > 0)
            {
                Response.Redirect("CaiGou_FuKuan_In.aspx?ID=" + GridView2.DataKeys[GridView2.SelectedIndex].Values[0].ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView2.SelectedIndex >= 0 && GridView2.Rows.Count > 0)
            {
                this.SqlDataSource3.DeleteCommand = "Exec CaiGou_FuKuan_Delete " + GridView2.DataKeys[GridView2.SelectedIndex].Values[0].ToString();
                this.SqlDataSource3.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource3.Delete();
                this.SqlDataSource3.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }     
    }
}
